#!/usr/bin/env python
#
# front_tab.py
#
# Version : 0.7
# Date : 20080705
#
# Author : Bernard Czenkusz
# Email  : bernie@skipole.co.uk

# front_tab.py - Displays the initial gui tab panel and is a module in sm_gui
# package, run as part of the skipolemonitor gui interface
#
# Copyright (c) 2007 Bernard Czenkusz
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

""" This is a module in the sm_gui package, and defines the first tab panel

It is run as part of the skipolemonitor gui interface.
"""


import wx
import wx.lib.hyperlink as hl
import os
from net import *
from sm_gui import gui_globals
from sm_gui import traffic_lights


class SkipoleMonitor(wx.Panel):
    """The first Skipole Monitor panel"""
    #
    # This is the introduction text
    introduction_text = """
Skipole Monitor : pings hosts, and displays their status via a
built-in web server, and optionally sends email and syslog alerts

Choose the Hosts and Groups tab to add and edit hosts.

The viewer should call this PC name or IP address with their
browser, """
    #
    # Note, it is displayed with extra text which depends on the
    # web server port which is being listenned on, this is added
    # within the class.

    # This is the license text displayed in the 'About Box' called
    # when the about button is pressed

    license_text="""
This program is free software; you can redistribute
it and/or modify it under the terms of the GNU
General Public License as published by the Free
Software Foundation; either version 2 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU
General Public License along with this program;
if not, write to
The Free Software Foundation, Inc.,
59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA"""

    # This is the description text displayed in the 'About Box' called
    # when the about button is pressed

    about_box_text = """
Pings hosts, and displays their status via a built-in web
server, and optionally sends email and syslog alerts."""

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
             
        self.main_sizer=wx.BoxSizer(wx.VERTICAL)

        self.port = net_cfg["PORT"]
      
        # Create top introduction text
        if self.port == "80":
            text = SkipoleMonitor.introduction_text + "for example:"
        else:
            text = SkipoleMonitor.introduction_text + "using port %s, for example:" % self.port

        self.top_text=wx.StaticText(self, -1, text)
        self.main_sizer.Add(self.top_text, 0, wx.ALL | wx.ALIGN_CENTER, 20)
        
        # Add hyperlink to introduction text
        if self.port == "80":
            self.hyper1 = hl.HyperLinkCtrl(self, wx.ID_ANY,
                                           "http://localhost",
                                           URL="http://localhost/")
        else:
            self.hyper1 = hl.HyperLinkCtrl(self, wx.ID_ANY,
                                           "http://localhost:%s" % self.port,
                                           URL="http://localhost:%s/" % self.port)
        self.hyper1.SetToolTipString("Open browser at this web page")
        self.main_sizer.Add(self.hyper1, 0, wx.BOTTOM | wx.ALIGN_CENTER, 30)

        # set the host status counts and icons
        self.set_hoststatus()

        # Create the main function buttons and text in a grid sizer
        self.buttons_sizer=wx.FlexGridSizer(rows=0, cols=2, hgap=20, vgap=20)
        button_size = (100,40)

        # About
        self.about_button = wx.Button(self, label="&About", size=button_size)
        self.buttons_sizer.Add(self.about_button, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        about_text=wx.StaticText(self, -1, "Version and Licence")
        self.buttons_sizer.Add(about_text, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)

        # Show
        self.show_button = wx.Button(self, label="&Status", size=button_size)
        self.buttons_sizer.Add(self.show_button, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        show_text=wx.StaticText(self, -1, "Display Red Yellow Green icons\nin a separate window.")
        self.buttons_sizer.Add(show_text, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)

        # Exit
        self.exit_button = wx.Button(self, wx.ID_EXIT, size=button_size)
        self.buttons_sizer.Add(self.exit_button, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        exit_text=wx.StaticText(self, -1, "Exit, and shut down, hosts\nwill no longer be polled.")
        self.buttons_sizer.Add(exit_text, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)

        # Add grid buttons_sizer to the main sizer
        self.main_sizer.Add(self.buttons_sizer, 0, wx.ALL | wx.ALIGN_CENTER, 40)

        # Set the size to the panel
        self.SetSizer(self.main_sizer)

        # Button Bindings
        self.Bind(wx.EVT_BUTTON, self.about_intro, self.about_button)
        self.Bind(wx.EVT_BUTTON, self.show_box, self.show_button)
        # The exit_button event is bound at the frame level - since
        # it calls the frame closer

    def set_hoststatus(self):
        """Set the host status counts"""
        # images are kept in net_cfg["IMAGES_FOLDER"]
        global net_cfg
        global HOST_STATS
        # Create sizer for status images
        self.status_sizer=wx.BoxSizer(wx.HORIZONTAL)
        # Add left space the to sizer
        self.status_sizer.Add((80,40), 1)
        # RED HOSTS
        red_png=os.path.join(net_cfg["IMAGES_FOLDER"],'redhost.png')
        red_image= wx.Image(red_png, wx.BITMAP_TYPE_PNG).Scale(20, 20)
        red_square=wx.StaticBitmap(self, -1, wx.BitmapFromImage(red_image))
        self.status_sizer.Add(red_square, 0)
        self.red_text=wx.StaticText(self, -1, " : %i" % HOST_STATS[1])
        self.status_sizer.Add(self.red_text, 0)
        self.status_sizer.Add((40,40), 1)
        # YELLOW HOSTS
        yellow_png=os.path.join(net_cfg["IMAGES_FOLDER"],'yellowhost.png')
        yellow_image= wx.Image(yellow_png, wx.BITMAP_TYPE_PNG).Scale(20, 20)
        yellow_square=wx.StaticBitmap(self, -1, wx.BitmapFromImage(yellow_image))
        self.status_sizer.Add(yellow_square, 0)
        self.yellow_text=wx.StaticText(self, -1, " : %i" % HOST_STATS[2])
        self.status_sizer.Add(self.yellow_text, 0)
        self.status_sizer.Add((40,40), 1)
        # GREEN HOSTS
        green_png=os.path.join(net_cfg["IMAGES_FOLDER"],'greenhost.png')
        green_image= wx.Image(green_png, wx.BITMAP_TYPE_PNG).Scale(20, 20)
        green_square=wx.StaticBitmap(self, -1, wx.BitmapFromImage(green_image))
        self.status_sizer.Add(green_square, 0)
        self.green_text=wx.StaticText(self, -1, " : %i" % HOST_STATS[3])
        self.status_sizer.Add(self.green_text, 0)
        self.status_sizer.Add((80,40), 1)
        # Add status_sizer to the main sizer
        self.main_sizer.Add(self.status_sizer, 0,  wx.EXPAND)
        #
        # Check status every two seconds
        self.status_checker = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnChecking, self.status_checker)
        self.status_checker.Start(2000, oneShot=False)

    def OnChecking(self, event):
        """Check the host statisticts"""
        global HOST_STATS
        # set the values on the mini traffic lights window
        self.red_text.SetLabel(" : %i" % HOST_STATS[1])
        self.yellow_text.SetLabel(" : %i" % HOST_STATS[2])
        self.green_text.SetLabel(" : %i" % HOST_STATS[3])
        #
        # If necessary, renew the top text of the page
        if self.port == net_cfg["PORT"]: return
        # The port value has changed, therefore change the text on the front page
        # and the hyperlink
        self.port = net_cfg["PORT"]
        if self.port == "80":
            text = SkipoleMonitor.introduction_text + "for example:"
            self.hyper1.SetLabel("http://localhost")
            self.hyper1.SetURL("http://localhost")
        else:
            text = SkipoleMonitor.introduction_text + "using port %s, for example:" % self.port
            self.hyper1.SetLabel("http://localhost:%s" % self.port)
            self.hyper1.SetURL("http://localhost:%s" % self.port)
        self.top_text.SetLabel(text)


    def about_intro(self, event):
        """About button pressed - Aboutbox or Message box depending on wxPython version"""
        global net_cfg
        #
        text = SkipoleMonitor.about_box_text
        #
        # AboutBox widgit is only available from wxpython version 2.7
        # so find the current version
        try:
            version_string = wx.version()
            x=int(version_string[0])
            y=int(version_string[2])
        except:
            x=0
            y=0
        if x>2 or (x==2 and y>6):
            # Create an AboutBox
            # First we create and fill the info object
            info = wx.AboutDialogInfo()
            info.Name = "Skipole Monitor"
            info.Version = gui_globals.SM_VERSION
            info.Copyright = "(C) Bernard Czenkusz"
            info.Description = text
            info.WebSite = ("http://www.skipole.net", "http://www.skipole.net")
            info.Developers = [ "Bernard Czenkusz  :  bernie@skipole.co.uk" ]
            info.License = SkipoleMonitor.license_text
            # Then we call wx.AboutBox giving it that info object
            wx.AboutBox(info)
        else:
            # Create a MessageBox
            text += "\nSkipole Monitor Version " + gui_globals.SM_VERSION + "\n(C) Bernard Czenkusz\nLicense GPL\nwww.skipole.net\n"
            wx.MessageBox(parent=self, message=text,
                          caption="About Skipole Monitor",
                          style=wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP)

    def show_box(self, event):
        """Show status box button pressed - display the traffic lights window"""
        #
        # Opens up the TrafficLights window, as defined in the traffic_lights module
        if traffic_lights.STATUS_BOX == 0:
            self.lights = traffic_lights.TrafficLights(self)
            self.lights.Show()


